using System;
using Jalal.Library.Data;
using System.Data;
using BCGC.DAL;
using Jalal.Library.Web;
using Jalal.Library.Utility;
using System.Data.SqlTypes;

namespace BCGC.BLL.BusinessObject.UserAccount
{
    public class SiteUser : UserBase
    {
        public override bool IsAdmin
        {
            get { return false; }
        }

        protected int roleId;
        public int RoleId
        {
            get { return roleId; }
        }

        protected bool isOnline;
        public bool IsOnline
        {
            get { return isOnline; }
            set { isOnline = value; }
        }

        protected bool isLocked;
        public bool IsLocked
        {
            get { return isLocked; }
            set { isLocked = value; }
        }

        protected bool isActivated;
        public bool IsActivated
        {
            get { return isActivated; }
        }

        protected string verificationCode;
        public string VerificationCode
        {
            get { return verificationCode; }
        }

        protected string secretQuestion;
        public string SecretQuestion
        {
            get { return secretQuestion; }
            set { secretQuestion = value; }
        }

        protected string secretAnswer;
        public string SecretAnswer
        {
            get { return secretAnswer; }
            set { secretAnswer = value; }
        }

        private UserProfile userProfile;
        public UserProfile Profile
        {
            get { return userProfile; }
            set { userProfile = value; }
        }

        public SiteUser()
        {
            this.id = 0;
            this.username = null;
            this.password = null;
            this.email = null;
            this.createDateTime = SqlDateTime.MinValue.Value;
            this.invalidLoginCount = 0;
            this.isLocked = false;
            this.isOnline = false;
            this.lastLoginTime = SqlDateTime.MinValue.Value;
            this.roleId = 0;
            this.secretAnswer = null;
            this.secretQuestion = null;
            this.userProfile = null;
            this.verificationCode = null;
        }

        public SiteUser(DataRow dataRow)
        {
            this.id = (int)dataRow["Id"];
            this.username = (string)dataRow["SiteUserName"];
            this.password = (string)dataRow["SiteUserPassword"];
            this.email = (string)dataRow["Email"];
            this.createDateTime = (DateTime)dataRow["CreateDateTime"];
            this.invalidLoginCount = (int)dataRow["InvalidLoginCount"]; ;
            this.isLocked = (bool)dataRow["IsLocked"];
            this.isOnline = (bool)dataRow["IsOnline"];
            this.lastLoginTime = (DateTime)dataRow["LastLoginTime"];
            this.roleId = (int)dataRow["RoleId"]; ;
            this.secretAnswer = (string)dataRow["SecretAnswer"];
            this.secretQuestion = (string)dataRow["SecretQuestion"];
            this.userProfile = null;                                        // Will be loaded by lazy loading
            this.verificationCode = (string)dataRow["VerificationCode"]; 
        }

        public override void Login(string username, string password)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);

            password = Security.Encrypt(password, SiteConfigSection.SiteConfiguration.EncryptionKey, true);
            DataTable data = UserData.GetSiteUserByLoginInfo(username, password);

            DataFactory.GetDataMiner().CommitTransaction();

            if (data != null && data.Rows.Count > 0)
            {
                SiteUser user = new SiteUser(data.Rows[0]);
                SessionManager.LoggedUser = user;
            }
        }

        public override void Logout()
        {
            SessionManager.LoggedUser = null;
        }

        public bool Register(string p, string p_2, string p_3, string p_4, string p_5)
        {
            throw new NotImplementedException();
        }
    }
}
